﻿using System;
using System.Collections.Generic;
using System.Text;
using ttlang.node;

namespace ttlang.builder.cs
{
    static class TypeConverter
    {
        readonly static Dictionary<string, string> tt2CSharpBaseType = new Dictionary<string, string>()
        {
            ["uint8"] = "byte",
            ["uint16"] = "ushort",
            ["uint32"] = "uint",
            ["int8"] = "sbyte",
            ["int16"] = "short",
            ["int32"] = "int",
            ["float"] = "float",
            ["float64"] = "double",
            //["char"] = "char",//沒有char
            ["bool"] = "bool",
            ["string"] = "string",
        };

        public static string ConvertType(string typeStr)
        {
           
            if (tt2CSharpBaseType.ContainsKey(typeStr))
            {
                return tt2CSharpBaseType[typeStr];
            }
            return typeStr;
        }

        public static bool TryConvertMethod(StringBuilder coder, Node_Expr_Call call)
        {
            Node_Expr_Member mem = call.function as Node_Expr_Member;
            if(mem!=null&&mem.member is ttlang.semantic.TTMethod tm)
            {
                
                if(tm.container.name=="Console"&&(tm.container.parentContainer==null|| tm.container.parentContainer.name==""))
                {
                    if(tm.name=="Warn")
                    {
                        coder.Append("ttbase.TTConsole.Warn");
                        cs.NodeExporter.BuildCallParams(coder, call.args);
                        return true;
                    }
                    if (tm.name == "Log")
                    {
                        coder.Append("ttbase.TTConsole.Log");
                        cs.NodeExporter.BuildCallParams(coder, call.args);
                        return true;
                    }
                    if (tm.name == "Error")
                    {
                        coder.Append("ttbase.TTConsole.Error");
                        cs.NodeExporter.BuildCallParams(coder, call.args);
                        return true;
                    }
                }
            }
            
            return false;
        }

    }
}
